From c7afa5452b2f32096d4dda4693e1a948ac1c8f42 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 14 Dec 2021 00:12:37 -0500 Subject: [PATCH] builder: Drop irrelevant whitespace in precompile Drop text nodes that won't contribute to the end result. This gets rid of a lot of text nodes in the replay. --- gtk/gtkbuilderprecompile.c | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/gtk/gtkbuilderprecompile.c b/gtk/gtkbuilderprecompile.c index 7c8dac9a5a..5128f3ab4b 100644 --- a/gtk/gtkbuilderprecompile.c +++ b/gtk/gtkbuilderprecompile.c @@ -58,6 +58,7 @@ struct RecordDataElement { RecordDataElement *parent; GQueue children; int n_attributes; + gboolean preserve_whitespace; RecordDataString *name; RecordDataString *attributes[]; }; @@ -92,6 +93,25 @@ record_data_node_new (RecordDataElement *parent, return node; } +static gboolean +text_is_important (const char *name) +{ + const char *elements[] = { + "property", + "attribute", + "col", + "action-widget", + "item", + "mime-type", + "pattern", + "suffix", + "mark", + NULL + }; + + return g_strv_contains (elements, name); +} + static RecordDataElement * record_data_element_new (RecordDataElement *parent, RecordDataString *name, @@ -105,6 +125,7 @@ record_data_element_new (RecordDataElement *parent, sizeof (RecordDataString) * n_attributes); element->parent = parent; element->name = name; + element->preserve_whitespace = name && text_is_important (name->string); element->n_attributes = n_attributes; return element; @@ -266,6 +287,23 @@ record_end_element (GMarkupParseContext *context, data->current = data->current->parent; } +static gboolean +is_whitespace (const char *text, + gsize text_len) +{ + const char *end; + const char *p; + + end = text + text_len; + for (p = text; p < end; p = g_utf8_next_char (p)) + { + if (!g_unichar_isspace (g_utf8_get_char (p))) + return FALSE; + } + + return TRUE; +} + static void record_text (GMarkupParseContext *context, const char *text, @@ -276,6 +314,9 @@ record_text (GMarkupParseContext *context, RecordData *data = user_data; RecordDataString *string; + if (!data->current->preserve_whitespace && is_whitespace (text, text_len)) + return; + string = record_data_string_lookup (data, text, text_len); record_data_element_append_text (data->current, string); } -- 2.30.2